473,419 Members | 4,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

read string and convert to bits?

Hi
I have a txt file full of words and numbers
for example:
-------------------------------------------------
`smash the stack` [C programming] n. On many C implementations
it is possible to corrupt the execution stack by writing past
the end of an array declared auto in a routine. Code that does
this is said to smash the stack, and can cause return from the
outine to jump to a random address. This can produce some of
-------------------------------------------------
i need to read the txt file char by char and convert it into bits(i.e 10100101010).
how can I do that?
Apr 23 '07 #1
6 7157
gpraghuram
1,275 Expert 1GB
HI,
1)Open the input file
2)use fgetc to a single character
3)sprintf it to a string using %d to get the ascii value
4)convert the bit to binary value
5)print that to another file

Hope this is what u want....

Thanks
Raghuram
Apr 23 '07 #2
HI,
1)Open the input file
2)use fgetc to a single character
3)sprintf it to a string using %d to get the ascii value
4)convert the bit to binary value
5)print that to another file

Hope this is what u want....

Thanks
Raghuram
hi
thanks for the quick reply
fgetc seem to use the FILE class
for the simplicity of my code I have used ofstream. is there a way to convert from ofstream to FILE?

thanks in advnaced
Apr 23 '07 #3
gpraghuram
1,275 Expert 1GB
Hi,
FILE is a C Library and ofstream u have used is C++
Check for a function from the class u have used to read a single character.
Check this page for refernce
Thanks
Raghuram
Apr 23 '07 #4
AdrianH
1,251 Expert 1GB
HI,
1)Open the input file
2)use fgetc to a single character
3)sprintf it to a string using %d to get the ascii value
4)convert the bit to binary value
5)print that to another file

Hope this is what u want....

Thanks
Raghuram
You can do this with streams as well. Use istream::operator >>(char) and ostream::operator <<(char). Using a file in this way is not necessarily efficient though and is not portable.

A better way would be to read in the binary string, and know the length from it. Then use the left shift operator to convert.

I.e convert an 8 bit string in pseudo code (approximate, not been tested):

Expand|Select|Wrap|Line Numbers
  1. bitstring = "00000000"
  2. value=0
  3. for i=0 to 7
  4.   value |= (bitstring[i] == '1')
  5. end for
Hope this helps.


Adrian
Apr 23 '07 #5
AdrianH
1,251 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. bitstring = "00000000"
  2. value=0
  3. for i=0 to 7
  4.   value |= (bitstring[i] == '1')
  5. end for
Err, it should read:

Expand|Select|Wrap|Line Numbers
  1. bitstring = "00000000"
  2. value=0
  3. for i=0 to 7
  4.   value |= ((bitstring[i] == '1') << i)
  5. end for
Note that this is fairly simplistic and can be done in a variety of different ways.

Adrian
Apr 23 '07 #6
blinktude do you still need "help"? Message me if you do...this is very interesting...

-Ari
Apr 28 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dmitri Shvetsov | last post by:
Hi All, Does somebody know what's the length's ratio in we convert byte to base64 string? Is it a constant ratio or it can be different from case to case? For example if I convert a byte array...
7
by: Jim | last post by:
I have to access a third party api to work with their hardware. Everything was going great UNTIL I ran into the problem of C# char being 16 bits and the C char being 8 bits. I cannot seem to...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
6
by: juli | last post by:
Hello dear Cor or anyone around!:) This didn't help me(convert) : I have 3 strings in an array : str='11/02/04' ,str='11:23:00" and str=AM. How do I convert str+str+str to a proper datetime...
3
by: James | last post by:
Hi, I am developing a ActiveX Control which will be used in a web page. The control will encrypt some value then decrypt it when the web page opens next time. I tested the control in a windows...
14
by: ern | last post by:
Does a function exist to convert a 128-bit hex number to a string?
2
by: duylam76 | last post by:
I'm programming a server and corresponding client application that sends data to each other over a socket. I want to encode an integer to send over the socket. Right now everything is fine because...
3
by: sam | last post by:
same as subject?
1
by: tom thomas | last post by:
Dear sir, i am very new to c# and i request you to... please look into the problem The following is the code(please see the code below:) which i uses to open the binary file,currently it works...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.